package com.example.wac.util;

import com.example.wac.constant.Constant;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author wuancheng
 * @description 获取当前登录用户的工具类
 * @date 2022-01-07 11:17:00
 */

public class CurrentUserUtils {

    /**
     * RequestContextHolder持有上下文的Request容器,类里有ThreadLocal保存当前线程下的request
     * 获取当前登录用户的id
     * @return
     */
    public static Long getUserId(){
        Long userId =Long.valueOf((String) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute(Constant.CURRENT_USER));
        return userId;
    }
}
